﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    public class ServiceAccountDayTradeInstrumentAnalsis : Benefit.Interface.Day.IAccountDayTradeInstrumentAnalsis
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountDayTradeInstrumentAnalsis(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 删除某日的所有分析数据
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.AccountDayTradeInstrumentAnalysis where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.AccountDayTradeInstrumentAnalysis d in query)
            {
                db.AccountDayTradeInstrumentAnalysis.Remove(d);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 保存账号日分析情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitAccountInstrumentDayAnalysis(int tradeHistoryId, string pdate)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            Interface.ServerData.IT_TradeDetail_History tth = new ServerData.ServiceT_TradeDetail_History(db);
            Interface.Sys.IInstrument iinstru = new Sys.ServiceInstrument(db);
            Interface.Sys.IAccount _account = new Sys.ServiceAccount(db);
            Interface.Day.IDayWinOrLoseTick iday = new Day.ServiceDayWinOrLoseTick(db);
            Interface.Config.IDefaultValue idv = new Config.ServiceDefaultValue(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            foreach (Models.ServerData.T_Account account in serverAccounts)
            {

                //   Data.ServerData.T_TradeDetail_History tth = new Data.ServerData.T_TradeDetail_History();
                List<string> instruments = tth.GetAccountDayInstrument(pdate, account.Id);
                foreach (string instrument in instruments)
                {
                    #region i为0是多，i为1是空
                    for (int i = 0; i < 2; i++)
                    {
                        Models.Day.AccountDayTradeInstrumentAnalysis change = new Models.Day.AccountDayTradeInstrumentAnalysis();

                        //Sys.Account _account = new Sys.Account();
                        change.AccountId = _account.GetAccountId(account.LoginAccount);
                        change.TradeHistoryId = tradeHistoryId;
                        change.Direction = i;
  
                        change.InstrumentId = iinstru.GetInstrumentId(instrument);
                        change.PingJinPing = tth.GetDayInstrumentPingCount(pdate, account.Id, instrument, change.Direction.ToString());
                        change.MaxReturn = tth.GetInstrumentMaxReturn(pdate, account.Id, instrument, change.Direction.ToString());
                        change.AvgPostionTime = -1;
     
                        change.OverStopLoss = iday.GetInstrumentOverStopLoss(pdate, account.Id, idv.GetOverLostTick(), change.InstrumentId, change.Direction);
                        db.AccountDayTradeInstrumentAnalysis.Add(change);
                    }
                    #endregion
                }
            }
            db.SaveChanges();
        }


        public List<Models.Day.AccountDayTradeInstrumentAnalysis> GetAccountDayTradeInstrumentAnalysis(int accountid, int tradeHistoryId)
        {
            List<Models.Day.AccountDayTradeInstrumentAnalysis> instruments = new List<Models.Day.AccountDayTradeInstrumentAnalysis>();
            var query = db.AccountDayTradeInstrumentAnalysis.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                instruments = query.ToList();
            }
            return instruments;
        }
    }
}